//
//  SKUDetailChart.m
//  ZITHER
//
//  Created by weixin on 2018/11/13.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "SKUDetailChart.h"

@implementation SKUDetailChart

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

-(void)setAllValues:(NSArray *)allValues{
    _allValues = allValues;
    [self setupUI];

}


- (void)setupUI {

    
    
    YASimpleGraphView *graphView = [[YASimpleGraphView alloc]init];
    graphView.frame = CGRectMake(-15, 0, self.frame.size.width-50,(kScreenW - 30)/345.0*237-10);
    graphView.backgroundColor = [UIColor whiteColor];
    graphView.allValues = self.allValues;
    graphView.allDates = self.allDates;
    graphView.defaultShowIndex = self.allDates.count-1;
    graphView.delegate = self;
    graphView.lineColor = baseColorTheme;
    graphView.lineWidth = 2;
    graphView.lineAlpha = 1.0;
    graphView.enableTouchLine = NO;
    
    
    /** 设置渐变 */
    [self colorspace:graphView];

    [self addSubview:graphView];
    [graphView startDraw];
}



-(void)colorspace:(YASimpleGraphView*)view{
    
    
    
    //设置渐变
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    size_t num_locations = 2;
    
    CGFloat locations[2] = { 0.0, 1.0 };
    
    
    //255,123,109
    UIColor *startColor = baseColorTheme;
    //247.104.127
    CGFloat *startColorComponents = (CGFloat *)CGColorGetComponents([startColor CGColor]);
    
    UIColor *endColor = [UIColor colorWithRed:250.0/250.0 green:250.0/250.0 blue:250.0/250.0 alpha:1];
    
    CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents([endColor CGColor]);
    
    CGFloat components[8] = {
        
        startColorComponents[0],
        
        startColorComponents[1],
        
        startColorComponents[2],
        
        startColorComponents[3],
        
        /*起始点的颜色是蓝色*/
        
        endColorComponents[0],
        
        endColorComponents[1],
        
        endColorComponents[2],
        
        endColorComponents[3]
        
        /*终止点的颜色是绿色*/
        
    };
    

    view.bottomGradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
}

//自定义X轴 显示标签索引
- (NSArray *)incrementPositionsForXAxisOnLineGraph:(YASimpleGraphView *)graph {
    return @[@0,@1,@2];
}

//Y轴坐标点数
- (NSInteger)numberOfYAxisLabelsOnLineGraph:(YASimpleGraphView *)graph {
    return 3;
}

//自定义popUpView
- (UIView *)popUpViewForLineGraph:(YASimpleGraphView *)graph {
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    label.backgroundColor = [UIColor colorWithRed:146/255.0 green:191/255.0 blue:239/255.0 alpha:1];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:10];
    label.textAlignment = NSTextAlignmentCenter;
    label.hidden = YES;
    return label;
}

//修改相应点位弹出视图
- (void)lineGraph:(YASimpleGraphView *)graph modifyPopupView:(UIView *)popupView forIndex:(NSUInteger)index {
    UILabel *label = (UILabel*)popupView;
    NSString *date = [NSString stringWithFormat:@"%@",self.allDates[index]];
    NSString *str = [NSString stringWithFormat:@" %@ \n %@元 ",date,self.allValues[index]];
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 40) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} context:nil];
    [label setFrame:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    label.textColor = [UIColor whiteColor];
    label.text = str;
    label.hidden = YES;
    }
@end
